import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
	state: () => ({
		token: localStorage.getItem("token") || "",
		userInfo: JSON.parse(localStorage.getItem("userInfo") || "{}"),
	}),

	getters: {
		isAdmin: (state) => state.userInfo.role === "Admin",
		isUser: (state) => state.userInfo.role === "User",
	},

	actions: {
		setToken(token) {
			this.token = token;
			localStorage.setItem("token", token);
		},

		setUserInfo(userInfo) {
			this.userInfo = userInfo;
			localStorage.setItem("userInfo", JSON.stringify(userInfo));
		},

		clearUserInfo() {
			this.token = "";
			this.userInfo = {};
			localStorage.removeItem("token");
			localStorage.removeItem("userInfo");
		},
	},
});
